<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="125px"
    >
      <el-form-item label="类型" prop="wageType">
          <el-select v-model="queryParams.wageType" placeholder="请选择类型" clearable>
            <el-option label="基本工资" value="0"></el-option>
            <el-option label="绩效工资" value="1"></el-option>
            <el-option label="分润金额" value="2"></el-option>
            <el-option label="试课补贴" value="3"></el-option>
            <el-option label="试课奖励" value="4"></el-option>
            <el-option label="提现" value="5"></el-option>
          </el-select>

      </el-form-item>
      <el-form-item label="用户手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="教练id" prop="coachId">-->
<!--        <el-input-->
<!--          v-model="queryParams.coachId"-->
<!--          placeholder="请输入教练id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="余额" prop="amount">-->
<!--        <el-input-->
<!--          v-model="queryParams.amount"-->
<!--          placeholder="请输入余额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="变动金额" prop="changeAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.changeAmount"-->
<!--          placeholder="请输入变动金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分润金额" prop="profitAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.profitAmount"-->
<!--          placeholder="请输入分润金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="变动详情" prop="changeDetails">-->
<!--        <el-input-->
<!--          v-model="queryParams.changeDetails"-->
<!--          placeholder="请输入变动详情"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" >
      <el-col :span="1.5"> 总收入金额：{{ totalAmount }} 元 </el-col>
      <el-col :span="1.5"> 账户余额：{{ myAmount }} 元 </el-col>
      <el-col :span="1.5"> 可提现金额：{{ cash }} 元 </el-col>
      <el-col :span="1.5"   v-if="name!='admin'">
        <el-button type="success" plain icon="el-icon-edit" size="mini" @click="handleAdd"
          >提现</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="收入" name="first">

        <el-table v-loading="loading" :data="AmountInfoList" >

          <el-table-column label="金额" align="center" prop="changeAmount" />
          <el-table-column label="类型" align="center" prop="wageType" >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.wageType == 0" type="success">基本工资</el-tag>
              <el-tag v-if="scope.row.wageType == 1" type="warning">绩效工资</el-tag>
              <el-tag v-if="scope.row.wageType == 2" type="info">分润金额</el-tag>
              <el-tag v-if="scope.row.wageType == 3" type="success">试课补贴</el-tag>
              <el-tag v-if="scope.row.wageType == 4" type="info">试课奖励</el-tag>
              <el-tag v-if="scope.row.wageType == 5" type="danger">提现</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="时间" align="center" prop="createTime" />
          <el-table-column label="来源课程"   align="center" prop="planClassName" />
          <el-table-column  label="学生姓名" align="center" prop="studentInfo.name" />
          <el-table-column  label="用户姓名" align="center" prop="nickName" />
          <el-table-column  label="用户手机号" align="center" prop="phonenumber" />
          <el-table-column   label="学管师" align="center" prop="studentInfo.learnName" />
          <el-table-column   label="上课教练" align="center" prop="studentInfo.coachName" />
          <el-table-column    label="详情" align="center" prop="changeDetails" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="支出" name="second">

        <el-table v-loading="loading" :data="AmountInfoList"  v-if="activeName !='first'">

          <el-table-column label="金额" align="center" prop="changeAmount" />
          <el-table-column label="类型" align="center" prop="wageType" >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.wageType == 0" type="success">基本工资</el-tag>
              <el-tag v-if="scope.row.wageType == 1" type="warning">绩效工资</el-tag>
              <el-tag v-if="scope.row.wageType == 2" type="info">分润金额</el-tag>
              <el-tag v-if="scope.row.wageType == 3" type="success">试课补贴</el-tag>
              <el-tag v-if="scope.row.wageType == 4" type="info">试课奖励</el-tag>
              <el-tag v-if="scope.row.wageType == 5" type="danger">提现</el-tag>
            </template>
          </el-table-column>

          <el-table-column  label="审核状态" align="center" prop="approvalInfo.status" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.c_approve_status" :value="scope.row.approvalInfo.status" />
            </template>
          </el-table-column>
          <el-table-column label="时间" align="center" prop="createTime" />
          <el-table-column  label="审核备注" align="center" prop="approvalInfo.remark" />
        </el-table>
      </el-tab-pane>
    </el-tabs>




    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改金额明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="可提现金额" prop="changeAmount" label-width="100px">
          <el-input v-model="cashAmount" disabled />
          <small-text>（单位：元）</small-text>
        </el-form-item>
        <el-form-item label="提现金额" prop="changeAmount" label-width="100px">
          <el-input v-model="form.changeAmount" placeholder="请输入提现金额"/>
          <small-text>（单位：元）</small-text>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAmountInfo,
  otherlistAmountInfo,
  getAmountInfo,
  delAmountInfo,
  addAmountInfo,
  updateAmountInfo,
  findCashAmount,
} from "@/api/course/AmountInfo";

export default {
  name: "AmountInfo",
  dicts: ['c_approve_status'],
  data() {
    return {

      activeName:"first",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总收入金额
      totalAmount: 0,
      // 账户余额
      myAmount: 0,
      // 可提现金额
      cashAmount: 0,
      // 可提现金额
      cash: 0,
      // 金额明细表格数据
      AmountInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identity: null,
        studentId: null,
        coachId: null,
        amount: null,
        changeAmount: null,
        countType: null,
        profitAmount: null,
        wageType: null,
        changeDetails: null,
        cashStatus: null,
        planClassName: null,
        phonenumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      name:""
    };
  },
  created() {
    const name = this.$store.getters.name;
    this.name=name;
    console.log("[ name ] >", name)
    this.queryParams.countType = 0;
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      if(tab.name=="first"){
        this.queryParams.countType = 0;
      }else if(tab.name=="second"){
        this.queryParams.countType = 1;
      }
      this.getList();
    },
    /** 查询金额明细列表 */
    getList() {
      this.loading = true;
      listAmountInfo(this.queryParams).then((response) => {
        this.AmountInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      const  kk=this.queryParams;
      kk.countType=0

      otherlistAmountInfo(kk).then((response) => {
        console.log("[ otherlistAmountInfo ] >", response);
        this.totalAmount = response.data.amountAllAdd;
        this.myAmount = response.data.amount;
        this.cash = response.data.cash;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        identity: null,
        studentId: null,
        coachId: null,
        amount: null,
        changeAmount: null,
        countType: null,
        profitAmount: null,
        wageType: null,
        changeDetails: null,
        cashStatus: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "提现";
      //提现
      this.form.wageType = "5";
      findCashAmount().then((res) => {
        this.cashAmount = res.data;
        console.log('[ res ] >', res)
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAmountInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改金额明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAmountInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAmountInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除金额明细编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAmountInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "course/AmountInfo/export",
        {
          ...this.queryParams,
        },
        `AmountInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
